package com.ztesoft.web.syncbaseinfo.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztesoft.core.cache.EhCacheService;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.iocp.dto.DataSyncPO;
import com.ztesoft.web.syncbaseinfo.service.BaseInfoService;

@Controller
@RequestMapping("syninfo")
public class BaseInfoController {

	private final static ZTEsoftLogManager LOGGER = ZTEsoftLogManager.getLogger(BaseInfoController.class);

	@Autowired
	private BaseInfoService baseInfoService;

	
    @Resource(name = "frameworkEhCacheService")
    private EhCacheService dmsCacheService;
	/**
	 * iocp通知同步请求
	 * 
	 * @param hostInfoVo
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("notice.do")
	@ResponseBody
	public int notice(DataSyncPO dataSyncPO, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		LOGGER.info("----IOCP通知请求参数----" + dataSyncPO.toUrlParams());
		// 扩展做校验处理
		if (dataSyncPO.getSubSystemId() != null) {
			/*
			 * 主机
			 */
			if ("DeleteHost".equals(dataSyncPO.getRequestType())) {
				LOGGER.info("----iocp通知同步请求主机信息 delete --" + dataSyncPO.getRequestType() + ",hostId= " + dataSyncPO.getHostId());
				dmsCacheService.evict("dmsCache", "HostInfo" + dataSyncPO.getHostId());
				baseInfoService.deleteHostInfo(dataSyncPO.getHostId());
			} else if ("AddHost".equals(dataSyncPO.getRequestType())
					|| "ModifyHost".equals(dataSyncPO.getRequestType())) {
				LOGGER.info("----iocp通知同步请求主机信息 add/modify --" + dataSyncPO.getRequestType() + ",hostId= " + dataSyncPO.getHostId());
				baseInfoService.addHostBaseInfo(dataSyncPO.getHostId());
			}

			/*
			 * 数据库
			 */
			if ("DeleteDBBaseInfo".equals(dataSyncPO.getRequestType())) {
				LOGGER.info("----iocp通知同步请求数据库信息 delete --" + dataSyncPO.getRequestType() + ",dbBaseId= "
						+ dataSyncPO.getDbBaseId());
				baseInfoService.deleteDBInfo(dataSyncPO.getDbBaseId());
			} else if ("AddDBBaseInfo".equals(dataSyncPO.getRequestType())
					|| "ModifyDBBaseInfo".equals(dataSyncPO.getRequestType())) {
				LOGGER.info("----iocp通知同步请求数据库信息 add/modify --" + dataSyncPO.getRequestType() + ",dbBaseId= "
						+ dataSyncPO.getDbBaseId());
				baseInfoService.addDBBaseInfo(dataSyncPO.getDbBaseId());
			}
		}
		return Integer.MAX_VALUE;
	}

}
